// pages/myself/send/SendCoupon.js
let network = null;
let utils = null;
let api = null;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
const app = getApp();

Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    isSubmit: false,
    selected_stores:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("RefreshNotification", this.refreshNotification, this)
    this.setData({
      BrandId: options.BrandId,
      // Brandname: options.name,
      // img: options.img,
      // count: options.count
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("RefreshNotification", this)

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  // /**
  //  * 弹出框开关
  //  */
  // toggleDialog() {
  //   this.setData({
  //     showDialog: !this.data.showDialog
  //   })
  // },

  /**
   * 选择适用门店
   */
  goChooseStore: function (e) {
    if (this.data.selected_storesarr){
      var selected = JSON.stringify(this.data.selected_storesarr)
    }else{
      var selected = '""'
    }
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/send/ChooseStore?BrandId=' + this.data.BrandId + '&selected=' + selected
      })
    }
  },

  //选择日期
  bindDateChange:function(e){
    var startorend = e.currentTarget.dataset.type;
    if (startorend==1){
      if (this.data.enddate && this.data.enddate < e.detail.value){
        this.setData({
          startdate: ''
        })
        this.showZanTopTips(this.prompt, '开始时间不能大于结束时间');
      }else{
        this.setData({
          startdate: e.detail.value
        })
      }
    }else{
      if (e.detail.value >= utils.formatDate02(new Date())){
        if (this.data.startdate && this.data.startdate > e.detail.value) {
          this.setData({
            enddate: ''
          })
          this.showZanTopTips(this.prompt, '结束时间不能小于开始时间');
        } else {
          this.setData({
            enddate: e.detail.value
          })
        }
      }else{
        this.showZanTopTips(this.prompt, '结束时间不能小于当前时间');
      }  
    }
    isSubmit(this)
  },

  //提交添加优惠券
  formSubmit:function(e){
    this.setData({
      name: e.detail.value.name,
      comment: e.detail.value.comment
    })
    var postdata = {
      image: this.data.uploadimg,
      name: e.detail.value.name,
      startTime: this.data.startdate,
      endTime: this.data.enddate,
      useTime: e.detail.value.useTime,
      comment: e.detail.value.comment,
      useRange: this.data.selected_stores,
      brandId: this.data.BrandId
    }
    isSubmit(this)
    if (this.data.isSubmit){
      network._post_json(this, api.addCoupon, postdata, formSuccess);
    }else{
      this.showZanToast('请填写完整信息')
    }
  },

  /**
   * 进入商品
   */
  goCouponDetails:function(e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/discount/CouponDetails'
      })
    }
  },
  //上传优惠券图片
  uploadimg: function (e) {
    var that = this;
    var choose_type = e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.upload,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
          },
          success: function (res) {
            var upimg = JSON.parse(res.data);
              that.setData({
                uploadimg: upimg.data
              })
              isSubmit(that)
          }
        })
      },
    })

  },
  /**
 * 刷新地址
 */
  refreshNotification: function () {
    this.setData({
      selected_stores: wx.getStorageSync('selected_stores'),
      selected_storesarr: wx.getStorageSync('selected_stores').split(",")
    })
  }
}))

var formSuccess = function(that,res){
  if(res.data.data){
    that.showZanToast('发布优惠券成功！');
    WxNotificationCenter.postNotificationName("RefreshDownCoupon", false);
    wx.navigateBack();
      // wx.navigateTo({
      //   url: '/pages/myself/merchant/MerchantCoupon?BrandId=' + that.data.BrandId + '&name=' + that.data.Brandname + '&img=' + that.data.img + '&count=' + that.data.count + '&index=1'
      // })
 
  }
}

var isSubmit = function(that){
  if (that.data.uploadimg && that.data.name && that.data.startdate && that.data.enddate && that.data.selected_stores.length > 0 && that.data.comment.length>0){
    that.setData({
      isSubmit: true
    })
  }else{
    that.setData({
      isSubmit:false
    })
  }
}